<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * 订单基本模型
 *
 * Created by PhpStorm.
 * User: li
 * Date: 15-10-29
 * Time: 上午10:40
 */
class Orders_model extends MY_Model
{
    function __construct(){
        parent::__construct();
        $this->_table_name = 'order';
    }
    function get_status($order_id){
        $this->db->where('order_id',$order_id);
        $this->db->select('status');
        $query = $this->db->get($this->_table_name);
        if($query->num_rows()==1){
            return $query->result_array()[0]['status'];
        }
        return NULL;
    }
    function order_payed($order_id,$serial){
        $this->db->trans_start();
        $this->order_pay($order_id,$serial);
        $this->order_serial($order_id,$serial);
        $this->db->trans_complete();
    }
    function order_pay($order_id,$serial){
        $this->db->where('order_id',$order_id);
        $this->db->set('status',2);// 'serial_number' =>  $serial
        $this->db->set('serial_number',$serial);
        $this->db->set('payed_time',date('Y-h-d H:i:s',time()));
        $this->db->update($this->_table_name);
        return $this->db->affected_rows()>0;
    }
    function order_serial($order_id,$serial,$func='alipay'){
        $data = array(
            'order_id'      =>  $order_id,
            'serial_number' =>  $serial,
            'func'          =>  $func,
            'created'       =>date('Y-h-d H:i:s',time())
        );
        $this->db->insert('payed',$data);
        return $this->db->affected_rows() ? $this->db->insert_id() : 0;
    }

    /*
     * 退款事务
     */
    function refund($order_id){
        $this->db->trans_start();
        $this->refund_order($order_id);
        $this->insert_serial($order_id);
        $this->db->trans_complete();
    }

    /**
     * 撤销订单
     */
    function refund_order($order_id){
        $this->db->set('status',7);
        $this->db->where('order_id',$order_id);
        $this->db->update($this->_table_name);
    }

    /**
     * 生成退款流水
     */
    function insert_serial($order_id){

    }


}